package org.csu.mypetstore.web.servlets;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.util.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class SearchAutoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CatalogService catalogService = new CatalogService();
        List<Product> allProducts = catalogService.getProductList();

        response.setContentType("text/json");//把下面转换的像json的字符串以json对象输出
        PrintWriter out = response.getWriter();

        String str = JSON.toJSONString(allProducts);//转换成符合json语法的字符串
        out.print(str);

        //养成好习惯，把流关闭
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
